package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.InsertNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 新建套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 新增套餐
        setmealMapper.insert(setmeal);


        // 在菜品和套餐关联表中新增关联数据
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes.size() != 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.insert(setmealDishes);
        } else {
            throw new InsertNotAllowedException(MessageConstant.DISH_NOT_BE_RELATED_BY_SETMEAL);
        }


    }

    /**
     * 分页查询套餐信息
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);


        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 更改套餐的售卖状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        // 套餐内如果有停售菜品，则该套餐无法上架

        // 查询套餐内的菜品停售数量
        Integer count = setmealMapper.getDishStopStatusBySetmealId(id);

        if (count == 0) {
            // 套餐内的菜品都是启售状态
            Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
            setmealMapper.update(setmeal);
        } else {
            // 套餐内的菜品有停售状态
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }


    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryInfoById(Long id) {
        // 根据id查询套餐信息
        Setmeal setmeal = setmealMapper.getInfoById(id);

        // 根据id查询套餐内的菜品信息
        List<SetmealDish> setmealDishList = setmealDishMapper.queryInfoBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }


    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 修改套餐信息
        setmealMapper.update(setmeal);

        // 修改套餐菜品关系，先把setmeal_dish表中关于该id的数据都删除，然后重新添加（list.size大于0）
        // 根据套餐id删除套餐菜品关系中关联的数据
        setmealDishMapper.delete(setmeal.getId());

        // 根据前端传来的套餐菜品数据，添加数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList.size() > 0) {
            for (SetmealDish sd : setmealDishList) {
                sd.setSetmealId(setmeal.getId());
            }

            setmealDishMapper.insert(setmealDishList);
        }
    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            // 不可删除在售状态的套餐
            Setmeal setmeal = setmealMapper.getInfoById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

            // 删除setmeal表
            setmealMapper.delete(id);

            // 删除setmeal_dish表
            setmealDishMapper.delete(id);
        }
    }


    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
